package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.common.SQLUtil;
import com.foreveross.dao.VisitorCheckInDAO;
import com.foreveross.vo.VisitorCheckIn;

@Component("visitorCheckInDAO")
public class VisitorCheckInDAOImp extends SQLUtil implements VisitorCheckInDAO {

	@Override
	public List<VisitorCheckIn> findVisitorCheckInList(String name,
			String phone, String user_id, String houseId, String code,
			String weixinId, int pageIndex, int pageSize,String userId) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select new VisitorCheckIn(model.id,model.houseUser,model.name,model.address,model.endTime,");
		queryString.append(" model.verifyTime,model.verifyStatus,model.wechatId,model.count,model.ticket,model.ticketId,model.randomId, ");
		queryString.append(" CASE WHEN model.endTime >= NOW() AND model.verifyStatus = 1 THEN '1' ELSE '0' END as states) ");
		queryString.append(" from VisitorCheckIn model where 1=1 ");
		if (!"".equals(name) && null != name) {
			queryString.append(" and model.name like '%" + name + "%'");
		}
		if (!"".equals(phone) && null != phone) {
			queryString.append(" and model.phone like '%" + phone + "%'");
		}
		if (!"".equals(user_id) && null != user_id) {
			queryString.append(" and model.houseUser.id='" + user_id + "'");
		}
		if (!"".equals(houseId) && null != houseId) {
			queryString.append(" and model.houseUser.hrHouses.id = '" + houseId
					+ "'");
		}
		if (!"".equals(code) && null != code) {
			queryString.append(" and model.randomId like '%" + code + "%'");
		}
		if (!"".equals(weixinId) && null != weixinId) {
			queryString.append(" and model.houseUser.wechatId = '" + weixinId
					+ "'");
		}
		this.addAuthorityStr(queryString, userId, "houseUser.hrHouses.id");
		queryString.append(" order by model.verifyStatus desc,model.endTime desc");
		return (List<VisitorCheckIn>) findObjectAll(queryString.toString(),
				pageIndex, pageSize);
	}

	@Override
	public long getVisitorCheckInListCount(String name, String phone,
			String user_id, String houseId, String code, String weixinId,String userId)
			throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select count(*) from VisitorCheckIn model where 1=1 ");
		if (!"".equals(name) && null != name) {
			queryString.append(" and model.name like '%" + name + "%'");
		}
		if (!"".equals(phone) && null != phone) {
			queryString.append(" and model.phone like '%" + phone + "%'");
		}
		if (!"".equals(user_id) && null != user_id) {
			queryString.append(" and model.houseUser.id='" + user_id + "'");
		}
		if (!"".equals(houseId) && null != houseId) {
			queryString.append(" and model.houseUser.hrHouses.id = '" + houseId
					+ "'");
		}
		if (!"".equals(code) && null != code) {
			queryString.append(" and model.randomId like '%" + code + "%'");
		}
		if (!"".equals(weixinId) && null != weixinId) {
			queryString.append(" and model.houseUser.wechatId = '" + weixinId
					+ "'");
		}
		this.addAuthorityStr(queryString, userId, "houseUser.hrHouses.id");
		return (Long) findObjectAll(queryString.toString()).get(0);
	}

	@Override
	public VisitorCheckIn getVisitorCheckIn(String id, String weixinId)
			throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select model from VisitorCheckIn model where  1=1  ");
		if (!"".equals(id) && null != id) {
			queryString.append(" and  id='" + id + "'");
		}
		if (!"".equals(weixinId) && null != weixinId) {
			queryString.append(" and wechatId='" + weixinId + "'");
		}
		return (VisitorCheckIn) findObejctByHql(queryString.toString());
	}

	@Override
	public VisitorCheckIn scanVisitorCheckIn(String ticketId) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select model from VisitorCheckIn model where  1=1  ");
		if (!"".equals(ticketId) && null != ticketId) {
			queryString.append(" and ticketId='" + ticketId + "'");
		}
		queryString.append(" and 1=1 ");
		return (VisitorCheckIn) findObejctByHql(queryString.toString());
	}	

	@Override
	@Transactional
	public void updateVisitorCheckInStateByDate(String date) throws Exception {
		StringBuffer queryString = new StringBuffer();
		queryString.append("update VisitorCheckIn set verifyStatus = 0 where verifyStatus =1 and endTime<'"+date+"' ");
		updateObejctByHql(queryString.toString());
	}	
}
